﻿using Teriri.File.Client.Maui.Helper;
using Teriri.File.Client.Maui.Page;
using Microsoft.Maui.ApplicationModel;
using Android;

namespace Teriri.File.Client.Maui
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();

            this.Permission();
            this.SetTimeoutWork();
        }

        private async void OnCounterClicked(object sender, EventArgs e)
        {
            await Navigation.PushModalAsync(new ServerConfigPage());
        }

        private async void OnConnectClicked(object sender, EventArgs e)
        {
            await Navigation.PushAsync(new FilePage());
        }

        private async void OnGpsTest(object sender, EventArgs e)
        {
            await Navigation.PushAsync(new GpsTestPage());
        }

        private async void VerticalStackLayout_Loaded(object sender, EventArgs e)
        {
            Image_Hellow.Opacity = 0;
            await Image_Hellow.FadeTo(1, 1000);
        }

        /// <summary>
        /// 权限索要
        /// </summary>
        private async void Permission()
        {
            await Task.Delay(500);

            var msg = @"程序需要以下权限才可以运行：
    1.文件读写（文件上传和下载用）
    2.通知权限（额，其实没用到）";

            await DisplayAlert("提示", msg, "继续");

            var status = await Permissions.CheckStatusAsync<ReadWriteStoragePerms>();
            if (status != PermissionStatus.Granted)
            {
                status = await Permissions.RequestAsync<ReadWriteStoragePerms>();
                if (status != PermissionStatus.Granted)
                    await DisplayAlert("错误", "尚未授予文件读取权限！", "确定");
            }

            status = await Permissions.CheckStatusAsync<PostNotificationsPerms>();
            if (status != PermissionStatus.Granted)
            {
                status = await Permissions.RequestAsync<PostNotificationsPerms>();
                if (status != PermissionStatus.Granted)
                    await DisplayAlert("错误", "尚未授予通知权限！", "确定");
            }
        }

        private async void SetTimeoutWork()
        {
            await Task.Delay(1000);

            while (true)
            {
                await Task.Delay(100);

                var config = await ServerConfigHelper.GetConfig();

                if (config != null)
                {
                    Label_Tips.Text = "配置已加载";
                    ConnectServerBtn.IsVisible = true;
                }
                else
                {
                    Label_Tips.Text = "未配置服务器地址，点击下方按钮配置";
                    ConnectServerBtn.IsVisible= false;
                }
            }
        }
    }

#pragma warning disable CA1416 // 验证平台兼容性

    public class ReadWriteStoragePerms : Permissions.BasePlatformPermission
    {
        public override (string androidPermission, bool isRuntime)[] RequiredPermissions =>
            new List<(string androidPermission, bool isRuntime)>
            {
#if ANDROID33_0_OR_GREATER
                (global::Android.Manifest.Permission.ReadMediaAudio, true),
                (global::Android.Manifest.Permission.ReadMediaImages, true),
                (global::Android.Manifest.Permission.ReadMediaVideo, true),
#else
                (global::Android.Manifest.Permission.ReadExternalStorage, true),
                (global::Android.Manifest.Permission.WriteExternalStorage, true)
#endif
            }.ToArray();
    }

    public class PostNotificationsPerms : Permissions.BasePlatformPermission
    {
        public override (string androidPermission, bool isRuntime)[] RequiredPermissions =>
            new List<(string androidPermission, bool isRuntime)>
            {
                (global::Android.Manifest.Permission.PostNotifications, true)
            }.ToArray();
    }

#pragma warning restore CA1416 // 验证平台兼容性
}